package com.ihrm.company.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.response.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.ihrm.company.controller
 * @date 2020/12/11 22:07
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 * 企业部门控制层
 */
    @RestController
@RequestMapping("/company")
public class DepartmentController extends BaseController {
    /**
     * 新增部门信息
     */
    @Autowired
    DepartmentService departmentService;
    @Autowired
    CompanyService companyService;

    /**
     * 新增企业部门
     *
     * @param department 部门信息
     * @return 添加结果
     */
    @PostMapping("/department")
    public Result addDepartment(@RequestBody Department department) {
        department.setCompanyId(companyId);
        departmentService.saveDepartment(department);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 修改企业部门信息
     *
     * @param id         部门id
     * @param department 修改信息
     * @return 修改结果
     */
    @PutMapping("/department/{id}")
    public Result updateDepartment(@PathVariable String id, @RequestBody Department department) {
        department.setId(id);
        departmentService.updateDepartment(department);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询部门信息
     *
     * @param id 部门id
     * @return 查询结果
     */
    @GetMapping("/department/{id}")
    public Result findById(@PathVariable String id) {
        Department department = departmentService.findById(id);
        return new Result(ResultCode.SUCCESS, department);
    }

    /**
     * 根据id删除部门信息
     *
     * @param id 部门id
     * @return 删除结果
     */
    @DeleteMapping("/department/{id}")
    public Result deleteById(@PathVariable String id) {
        departmentService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据企业id查询全部部门信息
     * @return 查询结果
     */
    @GetMapping("/department")
    public Result findAll() {
        //获取企业对象
        Company company = companyService.getCompanyById(companyId);
        //获取部门列表
        List<Department> departmentList = departmentService.findAllByCompanyId(companyId);
        //构建返回结果对象
        DeptListResult deptListResult = new DeptListResult(company, departmentList);
        return new Result(ResultCode.SUCCESS, deptListResult);
    }

    /**
     * 根据部门编号和企业id查询部门信息
     * @param code      部门编号
     * @param companyId 企业id
     * @return 部门对象
     */
    @PostMapping("/department/search")
    public Department findByCodeAndCompanyId(@RequestParam(value ="code") String code,
                                                @RequestParam(value = "companyId") String companyId) {
        return departmentService.findByCodeAndCompanyId(code, companyId);
    }
}
